package com.base.cn.platform.os.controller.statistics.goods;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.goods.GoodsPayRankingCount;
import com.base.cn.platform.os.entity.statistics.goods.ZStatisticsGoodsUVCount;
import com.base.cn.platform.os.entity.statistics.goods.ZStatisticsSubjectStudyUVCount;
import com.base.cn.platform.os.service.course.book.BookService;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.topic.ClassesTopicService;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.exam.paper.ExamPaperService;
import com.base.cn.platform.os.service.exam.question.ExamQuestionsService;
import com.base.cn.platform.os.service.exam.record.ExamRecordService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherBiz;
import com.base.cn.platform.os.service.statistics.base.ZStatisticsOrderBaseCountBiz;
import com.base.cn.platform.os.service.statistics.goods.ZStatisticsGoodsUVCountBiz;
import com.base.cn.platform.os.service.statistics.goods.ZStatisticsSubjectStudyUVCountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品统计Controller
 *
 * @author s.li
 * @date 2019/04/23
 */
@RestController
@RequestMapping("/manage/statistics")
public class GoodsStatisticsController extends BaseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private BookService bookService;
    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private ExamQuestionsService examQuestionsService;
    @Autowired
    private ExamPaperService examPaperService;
    @Autowired
    private SysTeacherBiz sysTeacherBiz;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesTopicService classesTopicService;
    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private ZStatisticsGoodsUVCountBiz zStatisticsGoodsUVCountBiz;
    @Autowired
    private ZStatisticsOrderBaseCountBiz zStatisticsOrderBaseCountBiz;
    @Autowired
    private ExamRecordService examRecordService;
    @Autowired
    private ZStatisticsSubjectStudyUVCountBiz zStatisticsSubjectStudyUVCountBiz;

    /**
     * 商品概况统计内容
     * @return Map<String,Object>
     */
    @RequestMapping("/queryGoodsProfileCountData")
    public synchronized Map<String,Object> queryGoodsProfileCountData(){
        //查询课程总数量数据
        Map<String,Integer> courseSumCountMap = courseService.queryCourseSumCount();
        //查询图书总数量数据
        Map<String,Integer> bookSumCountMap = bookService.queryBookSumCount();
        //查询免费直播总数量数据
        Map<String,Integer> courseOpenSumCountMap = courseOpenService.queryCourseOpenSumCount();
        //讲师
        BigDecimal _teacherCount = sysTeacherBiz.count(" `status` =1");
        if(!DataUtil.idIsNotNull(_teacherCount)){
            _teacherCount = BigDecimal.ZERO;
        }
        int teacherCount = _teacherCount.intValue();

        //班级数量
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        BigDecimal _classCount = classesService.getClassCount(params);
        int classCount = _classCount.intValue();

        //班级话题数量
        params.put("status",1);
        int classTopicCount = classesTopicService.getClassTopicCount(params).intValue();

        //查询问答总数量数据
        Map<String,Integer> qaSumCountMap = qaQuestionService.queryQaSumCount();

        //返回结果=========================================================================
        //在售商品总量
        int sellGoodsCount = 0;
        sellGoodsCount +=courseSumCountMap.get("sellCourseNum");//在售课程总量
        sellGoodsCount +=bookSumCountMap.get("sellBookNum");//在售图书总量

        //总商品数
        int goodsSum = 0;
        goodsSum += courseSumCountMap.get("courseNum");//课程总量
        goodsSum += bookSumCountMap.get("bookNum");//图书总量

        Map<String,Object> resultMap = ResultUtil.SUCCESS();
        resultMap.put("courseSumCountMap",courseSumCountMap);
        resultMap.put("bookSumCountMap",bookSumCountMap);
        resultMap.put("courseOpenSumCountMap",courseOpenSumCountMap);
        resultMap.put("teacherCount",teacherCount);
        resultMap.put("classCount",classCount);
        resultMap.put("classTopicCount",classTopicCount);
        resultMap.put("qaSumCountMap",qaSumCountMap);
        resultMap.put("sellGoodsCount",sellGoodsCount);
        resultMap.put("goodsSum",goodsSum);
        return resultMap;
    }


    /**
     * 访客榜 TOP5 之 商品
     * @return Map<String,Object>
     */
    @RequestMapping("/queryUVTopFiveGoodsList")
    public Map<String,Object> queryUVTopFiveGoodsList(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        List<ZStatisticsGoodsUVCount> goodsUVCountList = zStatisticsGoodsUVCountBiz.queryUVTopFiveGoodsList(dateQueryCondition);
        return ResultUtil.SUCCESS(goodsUVCountList);
    }

    /**
     * 查询支付榜 TOP5 之 付费商品
     * @param dateQueryCondition
     * @return Map<String,Object>
     */
    @RequestMapping("/queryGoodsPayTopFiveList")
    public Map<String,Object> queryGoodsPayTopFiveList(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        List<GoodsPayRankingCount> goodsPayRankingCountList = zStatisticsGoodsUVCountBiz.queryGoodsPayTopFiveList(dateQueryCondition);
        return ResultUtil.SUCCESS(goodsPayRankingCountList);
    }

    /**
     * 查询考试榜 TOP5 之 试卷
     * @param dateQueryCondition
     * @return List<ExamUseFiveCount>
     */
    @RequestMapping("/queryExamUseFiveList")
    public Map<String,Object> queryExamUseFiveList(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeWhereSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        List<Map<String,Object>> examUserFiveList = examRecordService.queryExamUseFiveList(dateList);
        if(ObjectUtils.isNotEmpty(examUserFiveList)){
            String examIds = examUserFiveList.stream().map(e->String.valueOf(e.get("examId"))).collect(Collectors.joining(","));
            Map<String,String> params = new HashMap<>();
            params.put("goodsType", ShopType.ORDER_SHOP_EXAM);
            params.put("timeSql", timeWhereSql);
            params.put("goodIds", examIds);
            List<ZStatisticsGoodsUVCount> goodsUVCountList = zStatisticsGoodsUVCountBiz.queryGoodsUVCountList(params);
            if(ObjectUtils.isNotEmpty(goodsUVCountList)){
                Map<BigDecimal,ZStatisticsGoodsUVCount> goodsUVCountMap = goodsUVCountList.stream().collect(Collectors.toMap(e->e.getGoodsId(),e->e));
                examUserFiveList.forEach(e->{
                    ZStatisticsGoodsUVCount uvCount = goodsUVCountMap.get(new BigDecimal(String.valueOf(e.get("examId"))));
                    if(uvCount !=null){
                        e.put("uvCount",uvCount.getUvCount());
                    }else{
                        e.put("uvCount",0);
                    }
                });
            }else{
                examUserFiveList.forEach(e->{
                    e.put("uvCount",0);
                });
            }
        }
        return ResultUtil.SUCCESS(examUserFiveList);
    }

    /**
     * 学习榜 TOP7之 专业/科目
     * @param dateQueryCondition 时间条件对象
     * @return Map<String,Object>
     */
    @RequestMapping("/querySubjectStudyRankingList")
    public Map<String,Object> querySubjectStudyRankingList(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeWhereSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        List<ZStatisticsSubjectStudyUVCount> subjectStudyUVCountList =zStatisticsSubjectStudyUVCountBiz.querySubjectStudyRankingList(timeWhereSql);
        return ResultUtil.SUCCESS(subjectStudyUVCountList);
    }

}
